<?php

class Category_Resource_Label extends WOW_Model_Resource_Db_Table_Abstract implements
Category_Resource_Label_Interface
{
    protected $_name = 'asset_category_labels';
    protected $_primary = 'labelid';
    protected $_rowClass = 'Category_Resource_Label_Item';

    protected $_dependentTables = array('Category_Resource_Subcategory');

    protected $_referenceMap = array(
        'Subcategory' => array(
            'columns' => 'subcatid',                                // FK to parent table ## here it is primary key ok SubCategory table
            'refTableClass' => 'Category_Resource_Subcategory',     // Parent table class 
            'refColumns' => 'subcatid',                              // col of cruuent table mapping to other table PK or FK
        ),

        'Category' => array(
            'columns' => 'catid',                                // FK to parent table ## here it is primary key ok SubCategory table
            'refTableClass' => 'Category_Resource_Category',     // Parent table class
            'refColumns' => 'catid',                              // col of cruuent table mapping to other table PK or FK
        )
    );
/*
    protected $_referenceMap = array(
        'Category' => array(
            'columns' => 'catid',                                // FK to parent table ## here it is primary key ok SubCategory table
            'refTableClass' => 'Category_Resource_Category',     // Parent table class
            'refColumns' => 'catid',                              // col of cruuent table mapping to other table PK or FK
        )
    );

*/
    public function getLabelById($id)
    {
        return $this->find($id)->current();
    }

    public function getAllLabels()
    {
        /*
        $this->_db->select()
                ->from('books' ,array('col1','col2') )
                ->joinInner('authors','books.id=authors.bks_id',array('col1','col2', 'col3'))
                ->where('where condition here')
                ->order('column name ASC/DESC');
         */
        $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('asset_category_labels', array('labelid', 'LabelName', 'asset_category_labels.subcatid', 'catid'))
                        ->join('asset_subcategories', 'asset_category_labels.subcatid = asset_subcategories.subcatid', array('SubCategoryName', 'subcat_ident' ))
                        ->order('LabelName');

        
        return $this->fetchAll($select);
    }

    public function getLabelByParentCategoryId($catid)
    {
        /*
         $select = $this->select()
                       ->where('catid = ?', $catid);
         */
          $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('asset_category_labels', array('labelid', 'LabelName', 'asset_category_labels.subcatid', 'catid'))
                        ->join('asset_subcategories', 'asset_category_labels.subcatid = asset_subcategories.subcatid')
                        ->where('asset_category_labels.catid = ?', $catid)
                        ->order('LabelName');


          return $this->fetchAll($select);
    }

    public function getLabelByParentSubcatId($subcatid)
    {
        /*
         $select = $this->select()
                       ->where('subcatid = ?', $subcatid);
       */
         $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('asset_category_labels', array('labelid', 'LabelName', 'asset_category_labels.subcatid', 'catid'))
                        ->join('asset_subcategories', 'asset_category_labels.subcatid = asset_subcategories.subcatid', array('SubCategoryName', 'subcat_ident' ))
                        ->where('asset_category_labels.subcatid = ?', $subcatid)
                        ->order('LabelName');

          return $this->fetchAll($select);
    }

    public function getLabelByParentCategoryIdent($ident)
    {
        // find parent category Id
        // find all the labels with the category id
        $cat_model = new Category_Resource_Category();
        $cat = $cat_model->getCategoryByIdent($ident);
        return $this->getLabelByParentCategoryId($cat->catid);
    }

    /**
     * finds the labels by parent sub-category ident
     * @param String $ident
     * @return Category_Resource_Subcategory
     */
    public function getLabelByParentSubcatIdent($subcat_ident)
    {
        // find parent sub-category ID by using $ident
        // then find all the labels by parent subcategory ID
        $subcat_model = new Category_Resource_Subcategory();
        $subcat = $subcat_model->getSubcategoryByIdent($subcat_ident);
        return $this->getLabelByParentSubcatId($subcat->subcatid);
    }

     /**
     * find the parent subcategory by label id
     * @param int $subcatid
     * @param String $subcat_ident
     * @return Category_Resource_Category | if $subcatid is null returns false
     */
    public function getParentSubCategory($labelid=null)
    {
       // $cat_model = new Category_Resource_Category();
        if($labelid != null)
        {
            $label = $this->getLabelById($labelid);
        }
        else
            return false;

        return $label->findParentRow('Category_Resource_Subcategory');
    }

    public function getParentCategory($labelid=null)
    {
       // $cat_model = new Category_Resource_Category();
        if($labelid != null)
        {
            $label = $this->getLabelById($labelid);
        }
        else
            return false;

        return $label->findParentRow('Category_Resource_Category');
    }

    /**
     * returns all the label matching the catid & subcatid
     * @param Integer $catid
     * @param Integer $subcatid
     */
    public function getLabelData($catid, $subcatid)
    {
        $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('asset_category_labels', array('labelid', 'LabelName' ))
                        ->join('asset_subcategories', 'asset_category_labels.subcatid = asset_subcategories.subcatid' )
                        ->join('asset_categories', 'asset_category_labels.catid = asset_categories.catid' )
                        ->where('asset_category_labels.catid = ?', $catid)
                        ->where('asset_category_labels.subcatid = ?', $subcatid)
                        ->order('LabelName');

        return $this->fetchAll($select);
    }

    
}

?>
